﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using WinFormAssigment.DAO;
using WinFormAssigment.Entities;

namespace WinFormAssigment.BUS
{
    class TeacherControl
    {
        public static bool InsertTeacherAvatar(string teacherID, byte[] image)
        {
            string sp = Query.teacherAvatar;
            SqlParameter[] sqlParameter = Query.TeacherAvatar(teacherID, image);
            bool result = DatabaseInteraction.InteractionData(sp, sqlParameter);
            return result;
        }
        public static byte[] GetTeacherAvatar(string teacherID)
        {
            byte[] arrByte = null;
            string sp = Query.getTeacherAvatar;
            SqlParameter[] sqlParameter = Query.GetTeacherAvatar(teacherID);
            DataTable table = DatabaseInteraction.GetSelectTable(sp, sqlParameter);
            int count = table.Rows.Count;
            if (count > 0)
            {
                arrByte = (byte[])table.Rows[count - 1]["Avatar"];
            }
            return arrByte;
        }
        public static bool ExistTeacheIDAvatar(string teacherID) {
            string sp = Query.getTeacherIDAvatar;
            SqlParameter[] sql = Query.GetTeacherIDAvatar(teacherID);
            DataTable table = DatabaseInteraction.GetSelectTable(sp,sql);
            int count = table.Rows.Count;
            if (count > 0)
            {
                return true;
            }
            else return false;
        }
        public static bool UpdateTeacherAvatar(string teacherID, byte[] image) {
            string sp = Query.updateTeacherAvatar;
            SqlParameter[] sqlParameter = Query.UpdateTeacherAvatar(teacherID,image);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;
        }
        public static bool DeleteTeacherAvatar(string teacherID) {
            string sp = Query.deleteTeacherAvatar;
            SqlParameter[] sqlParameter = Query.DeleteTeacherAvatar(teacherID);
            bool b = DatabaseInteraction.InteractionData(sp, sqlParameter);
            return b;
        }
        public static List<Teacher> GetAllTeacher()
        {
            List<Teacher> list = new List<Teacher>();
            string sp = Query.getAllTeacher;
            DataTable table = DatabaseInteraction.GetSelectTable(sp);
            foreach (DataRow row in table.Rows)
            {
                string teacherID = row[0].ToString();
                string password = row[1].ToString();
                string name = row[2].ToString();
                string gender = row[3].ToString();
                DateTime dob = Convert.ToDateTime(row[4].ToString());
                string address = row[5].ToString();
                string email = row[6].ToString();
                string phone = row[7].ToString();
                string description = row[8].ToString();
                Teacher teacher = new Teacher()
                {
                    TeacherID = teacherID,
                    Password = password,
                    Name = name,
                    Gender = gender,
                    Dob = dob,
                    Address = address,
                    Email = email,
                    Phone = phone,
                    Description = description
                };
                list.Add(teacher);
            }
            return list;
        }

        public static bool DeleteTeacher(String teacherID) {
            string sp = Query.deleteTeacher;
            SqlParameter[] sqlParameter = Query.DeleteTeacher(teacherID);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;
        }

        public static bool InsertTeacher(Teacher t) {
            string sp = Query.insertTeacher;
            SqlParameter[] sqlParameter = Query.InsertTeacher(t);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;
        }
        public static bool UpdateTeacher(Teacher t) {
            string sp = Query.updateTeacher;
            SqlParameter[] sqlParameter = Query.UpdateTeacher(t);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;
        }
        public static DataTable GetClassHistoryByTeacherID(string teacherID) {
            string sp = Query.getClassHistoryByTeacherID;
            SqlParameter[] sqlParameter = Query.GetClassHistoryByTeacherID(teacherID);
            DataTable table = DatabaseInteraction.GetSelectTable(sp,sqlParameter);
            return table;
        }
        public static List<Teacher> SearchTeacherByName(string teacherName) {
            List<Teacher> list = new List<Teacher>();
            string sp = Query.searchTeacherByName;
            SqlParameter[] sqlParameter = Query.SearchTeacherByName(teacherName);
            DataTable table = DatabaseInteraction.GetSelectTable(sp,sqlParameter);
            foreach (DataRow row in table.Rows)
            {
                string teacherID = row[0].ToString();
                string password = row[1].ToString();
                string name = row[2].ToString();
                string gender = row[3].ToString();
                DateTime dob = Convert.ToDateTime(row[4].ToString());
                string address = row[5].ToString();
                string email = row[6].ToString();
                string phone = row[7].ToString();
                string description = row[8].ToString();
                Teacher teacher = new Teacher()
                {
                    TeacherID = teacherID,
                    Password = password,
                    Name = name,
                    Gender = gender,
                    Dob = dob,
                    Address = address,
                    Email = email,
                    Phone = phone,
                    Description = description
                };
                list.Add(teacher);
            }
            return list;
        }
        public static List<string> GetAllTeacherID() {
            string sp = Query.getAllTeacherID;
            DataTable table = DatabaseInteraction.GetSelectTable(sp);
            List<string> list = new List<string>();
            foreach (DataRow row in table.Rows)
            {
                string teacherID = row["TeacherID"].ToString();
                list.Add(teacherID);
            }
            return list;
        }


    }
}
